package com.woniuxy.configuration;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;

import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.LogoutFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.woniuxy.filter.RolesFilter;
import com.woniuxy.realm.UserRealm;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;


@Configuration   //将当前类指定为配置类 等价于shiro的xml
public class ShiroConfiguration {
	//配置realm
	@Bean
	public UserRealm getReam(){
		UserRealm realm = new UserRealm();
		return realm;
	}
	//安全管理器
	@Bean
	public DefaultWebSecurityManager getManager(UserRealm realm){
		DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
		manager.setRealm(realm);
		return manager;
	}

	//过滤器
	@Bean
	public ShiroFilterFactoryBean getFactoryBean(DefaultWebSecurityManager manager){
		ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
		bean.setSecurityManager(manager);
		//登录页面
		bean.setLoginUrl("/user.html");
		//没有权限
		bean.setUnauthorizedUrl("/html/error.html");
		LogoutFilter logout = new LogoutFilter();
		logout.setRedirectUrl("/index.html");
		Map<String, Filter> filters=new HashMap<>();
		filters.put("logout",logout);
		bean.setFilters(filters);
		
		//过滤器链
		Map<String, String> map = new LinkedHashMap<>(); //需要保证有序

		
		map.put("/user.html", "anon");
		map.put("/bootstrap/**", "anon");
		map.put("/css/**", "anon");
		map.put("/img/**", "anon");
		map.put("/js/**", "anon");
		map.put("/lay/**", "anon");
		map.put("/font/**", "anon");
		map.put("/layui/**", "anon");
		map.put("/layui.js", "anon");
		map.put("/layui.all.js", "anon");
		map.put("/register.html", "anon");
		map.put("/header.html", "anon");
		map.put("/user/login", "anon");
		map.put("/user/tellogin", "anon");
		map.put("/logout", "logout");
		map.put("/**", "authc");
		
		bean.setFilterChainDefinitionMap(map);
		return bean;
	}
	@Bean
	public ShiroDialect getShiroDialect(){
		return new ShiroDialect();
	}
}
